package com.nami.gchat.controller.middleware.mq.consumer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.google.protobuf.InvalidProtocolBufferException;
import com.nami.gate.external.basic.bo.ConnectedUserBO;
import com.nami.gate.external.basic.constants.UpTopicConst;
import com.nami.gchat.facade.chat.GrpChatFacade;
import com.nami.gchat.facade.chat.request.GrpChatCmd;
import com.nami.im.network.model.message.v2.Message;
import com.nami.transport.proto.codec.ImMessages;
import com.nami.transport.proto.codec.MqTransMsg;
import com.nami.transport.utils.MsgTimeUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.dromara.common.json.utils.JsonUtils;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor
@RocketMQMessageListener(topic = UpTopicConst.C2G_TOPIC,
    consumerGroup = UpTopicConst.C2G_CONSUMER_GROUP,
    consumeMode = ConsumeMode.CONCURRENTLY)
public class GrpMsgConsumer implements RocketMQListener<MessageExt> {

    private final GrpChatFacade grpChatFacade;

    @Override
    public void onMessage(MessageExt msg) {
        try {
            TimeInterval timer = DateUtil.timer();
            GrpChatCmd cmd = parseGrpCmd(msg.getBody());

            if (cmd == null || MsgTimeUtil.isMessageExpired(cmd.getTimestamp())) {
                log.warn("[gchat] 消息过期，不予处理. mq消息消费成功！留存param={}", JsonUtils.toJsonString(cmd));
                return;
            }
            grpChatFacade.execute(cmd);
        } catch (Exception var3) {

        }

    }

    private GrpChatCmd parseGrpCmd(byte[] msg) throws InvalidProtocolBufferException {
        MqTransMsg.Router2LogicPublishMsg publishMsg = MqTransMsg.Router2LogicPublishMsg.parseFrom(msg);
        ImMessages.UpMsg upMsg = ImMessages.UpMsg.parseFrom(publishMsg.getData());;
        MqTransMsg.TransUser transSender = publishMsg.getSender();


        GrpChatCmd grpChatCmd = new GrpChatCmd();

        ConnectedUserBO sender = new ConnectedUserBO();
        sender.setInstanceId(transSender.getInstanceId());
        sender.setPlatform(transSender.getPlatform());
        sender.setAppKey(transSender.getAppKey());
        sender.setUserId(transSender.getUserId());
        sender.setConnectId(transSender.getConnectId());
        sender.setUserIp(transSender.getUserIp());


        Message message = new Message();
        message.setFlags(upMsg.getFlags());
        message.setMsgType(upMsg.getMsgType());
        message.setMsgContent(upMsg.getMsgContent().toStringUtf8());
        message.setMsgTime(upMsg.getMsgTime());
        message.setClientUid(upMsg.getClientUid());
        message.setChannelType(ImMessages.ChannelType.Group_VALUE);
        message.setTargetId(publishMsg.getTargetId());
        message.setSenderId(sender.getUserId());


        grpChatCmd.setSender(sender);
        grpChatCmd.setMessage(message);
        grpChatCmd.setTargetId(publishMsg.getTargetId());
        grpChatCmd.setTimestamp(publishMsg.getTimestamp());
        grpChatCmd.setQos(publishMsg.getQos());

        return grpChatCmd;
    }


}
